package com.android.webttest.Adapter;

import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.android.webttest.model.Good;
import com.android.webttest.model.GoodDetailInfo;
import com.android.webttest.model.HeatData;
import com.android.webttest.R;
import com.bumptech.glide.Glide;

import java.util.List;

import lombok.Setter;

public class GoodAdapter extends RecyclerView.Adapter<GoodAdapter.GoodViewHolder> {

    private List<Good> goodList;

    private List<GoodDetailInfo> goodDetailInfoList;

    private List<HeatData> heatDataList;


    @Setter
    private OnItemClickListener onItemClickListener;

    public GoodAdapter(List<Good> goodList, List<GoodDetailInfo> goodDetailInfoList, List<HeatData> heatDataList) {
        this.goodList = goodList;
        this.goodDetailInfoList = goodDetailInfoList;
        this.heatDataList = heatDataList;
    }

    @NonNull
    @Override
    public GoodViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_good, parent, false);
        return new GoodViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodViewHolder holder, int position) {
        Good good = goodList.get(position);
        holder.goodTitle.setText(good.getTitle());
        holder.goodPrice.setText(String.valueOf("售价:"+good.getPrice()+"元"));
        // 使用 stream API 过滤 goodDetailInfoList 并提取 goodInventory
        if (goodDetailInfoList != null) {
            goodDetailInfoList.stream()
                    .filter(g -> g.getGoodId().equals(good.getId()))
                    .findFirst()
                    .ifPresent(goodDetailInfo -> {
                        holder.goodInventory.setText(String.valueOf("库存:"+goodDetailInfo.getGoodInventory()+"件"));
                        holder.goodSellingPrice.setText(String.valueOf("现价:"+goodDetailInfo.getSellingPrice()+"元"));
                        holder.goodSoldNum.setText(String.valueOf("已售:"+goodDetailInfo.getSoldNum()+"件"));
                        // 设置点击事件
                        holder.itemView.setOnClickListener(v -> {
                            if (onItemClickListener != null) {
                                onItemClickListener.onItemClick(good, position);
                            }
                        });
                        if(goodDetailInfo.getSellingPrice()< good.getPrice()){
                            // 使用 ContextCompat 获取颜色值
                            holder.goodSellingPrice.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.hotPrice));
                            // 设置删除线
                            holder.goodPrice.setPaintFlags(holder.goodPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                        }
                    });
        } else {
            holder.goodInventory.setText("暂无数据");
            holder.goodSellingPrice.setText("暂无数据");
            holder.goodSoldNum.setText("暂无数据");
        }
        // 使用 heatDataList 过滤并提取 goodNowVisits
        if (heatDataList != null) {
            heatDataList.stream()
                    .filter(h -> h.getGoodId().equals(good.getId()))
                    .findFirst()
                    .ifPresent(heatData -> {
                        holder.goodNowVisits.setText(String.valueOf("关注人数:"+heatData.getNowVisit()+"人"));
                    });
        } else {
            holder.goodNowVisits.setText("暂无数据");
        }
        //holder.goodImage.setImageResource(good.getCoverImg());--需要的是资源id,不是url,要Glide
        // 使用 Glide 加载图片
        Glide.with(holder.itemView.getContext())
                .load(good.getCoverImg()) // 假设 getCoverImg() 返回图片路径或 URL
                .into(holder.goodImage);

    }

    @Override
    public int getItemCount() {
        return goodList.size();
    }

    public static class GoodViewHolder extends RecyclerView.ViewHolder {
        TextView goodTitle;
        TextView goodPrice;
        TextView goodInventory;
        TextView goodSellingPrice;
        TextView goodNowVisits;
        TextView goodSoldNum;

        ImageView goodImage;

        public GoodViewHolder(@NonNull View itemView) {
            super(itemView);
            goodImage = itemView.findViewById(R.id.goodImage);
            goodTitle = itemView.findViewById(R.id.goodTitle);
            goodPrice = itemView.findViewById(R.id.goodPrice);
            goodInventory = itemView.findViewById(R.id.goodInventory);
            goodSellingPrice = itemView.findViewById(R.id.goodSellingPrice);
            goodNowVisits = itemView.findViewById(R.id.goodNowVisits);
            goodSoldNum = itemView.findViewById(R.id.goodSoldNum);
        }
    }
    public void updateData(List<Good> goodList, List<GoodDetailInfo> goodDetailInfoList, List<HeatData> heatDataList) {
        this.goodList = goodList;
        this.goodDetailInfoList = goodDetailInfoList;
        this.heatDataList = heatDataList;
    }

    public interface OnItemClickListener {
        void onItemClick(Good good, int position);
    }
}